Passed
Pull Request — master (#142)
by
unknown
03:44
created

Page.render   A

Complexity

Conditions 2

Size

Total Lines 37
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 37
c 0
b 0
f 0
rs 9.064
cc 2
1
// @flow
2
3
import * as React from 'react';
4
import { connect } from 'react-redux';
5
import styled from 'styled-components';
6
import { push } from 'react-router-redux';
7
8
import BasicButton from 'components/elements/basic-button';
9
import SideNav from 'components/widgets/side-nav';
10
import { postActions } from 'controllers/actions/post';
11
import { getSelectedPost } from 'controllers/selectors/post';
12
import type { Post, PopulatedPost } from 'controllers/types/post';
13
14
import immutableToJS from 'utils/components/immutable-to-js';
15
16
type Props = {
17
  className: string,
18
  postList: [string],
19
  posts: { [string]: Post },
20
  selectedPost: PopulatedPost,
21
  navigate: (url: string) => void,
22
  get: () => void,
23
  select: (id: string) => void,
24
};
25
26
export class Page extends React.PureComponent<Props> {
27
  static defaultProps = {
28
    postList: [],
29
    posts: {},
30
    selectedPost: {
31
      title: '',
32
      body: '',
33
      comments: [],
34
    },
35
  };
36
37
  render() {
38
    const { className, postList, posts, selectedPost } = this.props;
39
    const { navigate, get, select } = this.props;
40
    return (
41
      <div className={className}>
42
        <BasicButton
43
          className="navButton"
44
          func={() => navigate('/')}
45
          text="Back to Index"
46
        />
47
        <BasicButton className="actionButton" func={get} text="Get Post List" />
48
        <div className="contentView">
49
          <SideNav
50
            className="titles"
51
            list={postList.map(id => ({ id, value: posts[id].title }))}
52
            func={select}
53
          />
54
          <div className="postContent">
55
            <div className="title">{selectedPost.title}</div>
56
            <div className="body">{selectedPost.body}</div>
57
          </div>
58
          <div className="comments">
59
            {selectedPost.comments.length ? (
60
              <div className="title">Comments</div>
61
            ) : null}
62
            <div className="body">
63
              {selectedPost.comments.map(({ _id, content, author }) => (
64
                <div key={_id} className="comment">
65
                  <div className="author">{author.name}:</div>
66
                  <div className="content">{content}</div>
67
                </div>
68
              ))}
69
            </div>
70
          </div>
71
        </div>
72
      </div>
73
    );
74
  }
75
}
76
77
const mapStateToProps = state => ({
78
  postList: state.getIn(['post', 'normalized', 'result']),
79
  posts: state.getIn(['post', 'normalized', 'entities', 'posts']),
80
  selectedPost: getSelectedPost(state),
81
});
82
83
const mapDispatchToProps = (dispatch: *) => ({
84
  navigate: url => dispatch(push(url)),
85
  get: () => dispatch(postActions.get()),
86
  select: id => dispatch(postActions.select(id)),
87
});
88
89
const component = styled(Page)`
90
  width: 640px;
91
  margin: 240px auto;
92
  line-height: 30px;
93
94
  .actionButton {
95
    background: lightblue;
96
    color: white;
97
  }
98
99
  .titles {
100
    display: inline-block;
101
    float: left;
102
    width: 120px;
103
  }
104
105
  .postContent {
106
    display: inline-block;
107
    float: left;
108
    width: 320px;
109
    margin: 20px;
110
    font-size: 14px;
111
112
    .title {
113
      font-weight: bold;
114
      line-height: 40px;
115
    }
116
  }
117
118
  .comments {
119
    margin-top: 20px;
120
    display: inline-block;
121
    float: left;
122
    width: 160px;
123
    font-size: 14px;
124
125
    .title {
126
      color: grey;
127
      line-height: 40px;
128
    }
129
130
    .comment {
131
      border-top: 1px solid lightgrey;
132
      padding: 5px 0;
133
      font-size: 13px;
134
135
      .author {
136
        color: darkgrey;
137
      }
138
139
      .content {
140
        color: grey;
141
      }
142
    }
143
  }
144
`;
145
146
export default connect(
147
  mapStateToProps,
148
  mapDispatchToProps,
149
)(immutableToJS(component));
150